package com.ml.doctor.activity;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.google.gson.reflect.TypeToken;
import com.ml.doctor.R;
import com.ml.doctor.bean.XDianBean;
import com.ml.doctor.network.NetworkApi;
import com.ml.doctor.network.NetworkManager;
import com.ml.doctor.utils.ToastUtil;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;

public class XDActivity extends BaseActivity {

    @BindView(R.id.listview)
    ListView listview;
    List<XDianBean> list;
    XDAdapter xdAdapter;
    @BindView(R.id.txt_non)
    TextView txtNon;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_xd);
        ButterKnife.bind(this);
        setTopTitle("心电记录");
        list = new ArrayList<>();
        xdAdapter = new XDAdapter();
        listview.setAdapter(xdAdapter);
        getListData();
    }

    private void getListData() {
        Map<String, String> map = new HashMap<>();
        map.put("bid", getIntParam("bid") + "");
        map.put("temp", 9 + "");
        NetworkManager.getInstance().getResultClass(NetworkApi.BasicUrl + "/br/cl_data", map, new TypeToken<List<XDianBean>>() {
        }.getType(), new NetworkManager.SuccessCallback<List<XDianBean>>() {
            @Override
            public void onSuccess(List<XDianBean> response) {
                list.clear();
                list.addAll(response);
                xdAdapter.notifyDataSetChanged();
            }
        }, new NetworkManager.FailedCallback() {
            @Override
            public void onFailed(String message) {
                ToastUtil.showShort(XDActivity.this, message);
                txtNon.setVisibility(View.VISIBLE);
            }
        });
    }

    class XDAdapter extends BaseAdapter {
        ViewHolder viewhold;

        @Override
        public int getCount() {
            return list.size();
        }

        @Override
        public Object getItem(int position) {
            return null;
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            if (convertView == null) {
                convertView = LayoutInflater.from(XDActivity.this).inflate(R.layout.item_xd, parent, false);
                viewhold = new ViewHolder(convertView);
                convertView.setTag(viewhold);
            }
            viewhold = (ViewHolder) convertView.getTag();
            viewhold.txtContent.setText("" + list.get(position).ecg);
            viewhold.txtTime.setText("监测时间：" + new SimpleDateFormat("yyyy-MM-dd HH:mm").format(new Date(list.get(position).time)));
            return convertView;
        }

        class ViewHolder {
            @BindView(R.id.txt_content)
            TextView txtContent;
            @BindView(R.id.txt_time)
            TextView txtTime;

            ViewHolder(View view) {
                ButterKnife.bind(this, view);
            }
        }
    }
}
